package com.senmo.fragment;

import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.senmo.base.BaseFragment;
import com.senmo.senmo.R;
import com.senmo.video.view.GLVideoView;

import lx.af.utils.NetStateUtils;
import lx.af.utils.ViewInject.ViewInject;

/**
 */
public class VideoPlayFragment extends BaseFragment implements View.OnClickListener {

    @ViewInject(id = R.id.fragment_videoview)
    private GLVideoView mVideoView;
    private Configuration configuration;

    private String mCurrentVideoUrl;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_video_play, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        int mScreenWidth = dm.widthPixels;
        int mScreenHeight = dm.heightPixels;
        configuration = new Configuration();
        if(mScreenHeight > mScreenWidth){
            configuration.orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
        }else{
            configuration.orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
        }
    }

    private void startVideoPlay(){
        if (mVideoView == null || getActivity() == null) {
            return;
        }
        if (mCurrentVideoUrl == null || mCurrentVideoUrl.equals("")) {
            return;
        }
        if (!NetStateUtils.isNetConnected()) {
            return;
        }

        mVideoView.stopPlay();
        mVideoView.startPlay(mCurrentVideoUrl);
    }

    @Override
    public void onDestroyView() {
        stopVideoPlay();
        super.onDestroyView();
    }

    public void stopVideoPlay(){
        if(mVideoView == null){
            return;
        }
        mVideoView.stopPlay();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (mVideoView == null) {
            return;
        }
        configuration = newConfig;
        mVideoView.resumeZoom();
    }

}
